Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

給自己看的 JS 進階-變數

給自己看的 JS 進階-變數

CSS保健室|background-attachment

CSS保健室|background-attachment

反向代理(Reverse proxy)、ORM 及 N+1 problem 介紹

反向代理(Reverse proxy)、ORM 及 N+1 problem 介紹






留言討論